tiantian-paris commited on
Commit
a1a9769
·
1 Parent(s): 912e356

update chatbot

Browse files
Files changed (2) hide show
  1. app.py +13 -4
  2. testUI.py +95 -0
app.py CHANGED
@@ -129,13 +129,22 @@ def chatbot_response_json(user_input):
129
 
130
 
131
  def chatbot_response(user_input, history):
 
132
  history = history or []
133
- response = chatbot_response_json(user_input) # Call the chatbot logic
134
  #history.append((user_input, response))
135
- return response
136
 
137
 
138
  # Create the Gradio interface
139
- demo = gr.ChatInterface(fn=chatbot_response, title="My Personal Chatbot", description="Ask me anything about myself!")
140
-
 
 
 
 
 
141
  demo.launch()
 
 
 
 
129
 
130
 
131
  def chatbot_response(user_input, history):
132
+ request = user_input
133
  history = history or []
134
+ response = chatbot_response_json(request) # Call the chatbot logic
135
  #history.append((user_input, response))
136
+ yield response
137
 
138
 
139
  # Create the Gradio interface
140
+ #demo = gr.ChatInterface(fn=chatbot_response, title="My Personal Chatbot", description="Ask me anything about myself!")
141
+ demo = gr.ChatInterface(
142
+ chatbot_response,
143
+ type="messages",
144
+ multimodal=False,
145
+ title="Ask me anything about.....",
146
+ )
147
  demo.launch()
148
+
149
+ if __name__ == "__main__":
150
+ demo.launch()
testUI.py ADDED
@@ -0,0 +1,95 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import anthropic
2
+ import base64
3
+ import gradio as gr
4
+ import mimetypes
5
+
6
+ def generate_playground_link(code):
7
+ """
8
+ Generate a Gradio app link from the code.
9
+ """
10
+ base64_code = base64.b64encode(code.encode()).decode()
11
+ return f"https://www.gradio.app/playground?code={base64_code}&reqs=&demo=Blank"
12
+
13
+ def extract_code(message):
14
+ """
15
+ Extract the code from a message that is formatted in Markdown with triple backticks.
16
+ Include the triple backticks in the return value.
17
+ """
18
+ return message.split('```python')[1].split('```')[0]
19
+
20
+
21
+ def get_response(data, history):
22
+ """
23
+ Get a response from the Anthropic API based on the image and user message.
24
+ """
25
+ client = anthropic.Anthropic()
26
+ response = []
27
+ if data["files"]:
28
+ response.append("Generating Gradio app based on the image...")
29
+ yield response
30
+ image = data["files"][0] # str filepath
31
+ image_data = base64.standard_b64encode(open(image, "rb").read()).decode("utf-8")
32
+ image_media_type = mimetypes.guess_type(image)[0]
33
+ message = client.messages.create(
34
+ model="claude-3-5-sonnet-20241022",
35
+ max_tokens=1024,
36
+ messages=[
37
+ {
38
+ "role": "user",
39
+ "content": [
40
+ {
41
+ "type": "image",
42
+ "source": {
43
+ "type": "base64",
44
+ "media_type": image_media_type,
45
+ "data": image_data,
46
+ },
47
+ },
48
+ {
49
+ "type": "text",
50
+ "text": "Based on the screenshot, write the Python code to generate this Gradio app. Try to recreate the Gradio UI, not necessarily the underlying data or machine learning model. Respond in Markdown enclosing the code in triple backticks."
51
+ }
52
+ ],
53
+ }
54
+ ],
55
+ )
56
+ else:
57
+ response.append("Generating Gradio app based on the description...")
58
+ yield response
59
+ request = data["text"]
60
+ message = client.messages.create(
61
+ model="claude-3-5-sonnet-20241022",
62
+ max_tokens=1024,
63
+ messages=[
64
+ {
65
+ "role": "user",
66
+ "content": [
67
+ {
68
+ "type": "text",
69
+ "text": request
70
+ },
71
+ {
72
+ "type": "text",
73
+ "text": "Based on the user's request, write the Python code to generate this Gradio app. Focus on the Gradio UI, not necessarily the underlying data or machine learning model. Respond in Markdown enclosing the code in triple backticks."
74
+ }
75
+ ],
76
+ }
77
+ ],
78
+ )
79
+ code = extract_code(message.content[0].text)
80
+ formatted_code = f"```python\n{code}\n```"
81
+ response.append(formatted_code)
82
+ yield response
83
+ response.append("[Try it out in the Gradio playground](" + generate_playground_link(code) + ")")
84
+ yield response
85
+
86
+
87
+ demo = gr.ChatInterface(
88
+ get_response,
89
+ type="messages",
90
+ multimodal=True,
91
+ title="Gradio Playground Bot",
92
+ )
93
+
94
+ if __name__ == "__main__":
95
+ demo.launch()