abidlabs HF staff commited on
Commit
296c0a2
·
1 Parent(s): 148a7d9
Files changed (1) hide show
  1. app.py +98 -0
app.py ADDED
@@ -0,0 +1,98 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import anthropic
2
+ import base64
3
+ import gradio as gr
4
+ import os
5
+ import mimetypes
6
+
7
+ os.environ["ANTHROPIC_API_KEY"] = "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"
8
+
9
+ def generate_playground_link(code):
10
+ """
11
+ Generate a Gradio app link from the code.
12
+ """
13
+ base64_code = base64.b64encode(code.encode()).decode()
14
+ return f"https://www.gradio.app/playground?code={base64_code}&reqs=&demo=Blank"
15
+
16
+ def extract_code(message):
17
+ """
18
+ Extract the code from a message that is formatted in Markdown with triple backticks.
19
+ Include the triple backticks in the return value.
20
+ """
21
+ return message.split('```python')[1].split('```')[0]
22
+
23
+
24
+ def get_response(data, history):
25
+ """
26
+ Get a response from the Anthropic API based on the image and user message.
27
+ """
28
+ client = anthropic.Anthropic()
29
+ response = []
30
+ if data["files"]:
31
+ response.append("Generating Gradio app based on the image...")
32
+ yield response
33
+ image = data["files"][0] # str filepath
34
+ image_data = base64.standard_b64encode(open(image, "rb").read()).decode("utf-8")
35
+ image_media_type = mimetypes.guess_type(image)[0]
36
+ message = client.messages.create(
37
+ model="claude-3-5-sonnet-20241022",
38
+ max_tokens=1024,
39
+ messages=[
40
+ {
41
+ "role": "user",
42
+ "content": [
43
+ {
44
+ "type": "image",
45
+ "source": {
46
+ "type": "base64",
47
+ "media_type": image_media_type,
48
+ "data": image_data,
49
+ },
50
+ },
51
+ {
52
+ "type": "text",
53
+ "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."
54
+ }
55
+ ],
56
+ }
57
+ ],
58
+ )
59
+ else:
60
+ response.append("Generating Gradio app based on the description...")
61
+ yield response
62
+ request = data["text"]
63
+ message = client.messages.create(
64
+ model="claude-3-5-sonnet-20241022",
65
+ max_tokens=1024,
66
+ messages=[
67
+ {
68
+ "role": "user",
69
+ "content": [
70
+ {
71
+ "type": "text",
72
+ "text": request
73
+ },
74
+ {
75
+ "type": "text",
76
+ "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."
77
+ }
78
+ ],
79
+ }
80
+ ],
81
+ )
82
+ code = extract_code(message.content[0].text)
83
+ formatted_code = f"```python\n{code}\n```"
84
+ response.append(formatted_code)
85
+ yield response
86
+ response.append("[Try it out in the Gradio playground](" + generate_playground_link(code) + ")")
87
+ yield response
88
+
89
+
90
+ demo = gr.ChatInterface(
91
+ get_response,
92
+ type="messages",
93
+ multimodal=True,
94
+ title="Gradio Playground Bot",
95
+ )
96
+
97
+ if __name__ == "__main__":
98
+ demo.launch()