Spaces:
Build error
Build error
contenteaseAI
commited on
Commit
•
fd3db54
1
Parent(s):
a487203
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,82 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from openai import OpenAI
|
3 |
+
import os
|
4 |
+
|
5 |
+
css = '''
|
6 |
+
.gradio-container{max-width: 1000px !important}
|
7 |
+
h1{text-align:center}
|
8 |
+
footer {
|
9 |
+
visibility: hidden
|
10 |
+
}
|
11 |
+
'''
|
12 |
+
|
13 |
+
ACCESS_TOKEN = os.getenv("HF_TOKEN")
|
14 |
+
|
15 |
+
client = OpenAI(
|
16 |
+
base_url="https://api-inference.huggingface.co/v1/",
|
17 |
+
api_key=ACCESS_TOKEN,
|
18 |
+
)
|
19 |
+
|
20 |
+
def respond(
|
21 |
+
message,
|
22 |
+
history,
|
23 |
+
max_tokens,
|
24 |
+
temperature,
|
25 |
+
system_message=""" Extract the following information from the given text:
|
26 |
+
Identify the specific areas where the work needs to be done and Add the furniture that has to be changed.
|
27 |
+
Do not specify the work that has to be done.
|
28 |
+
Format the extracted information in the following JSON structure:
|
29 |
+
|
30 |
+
{
|
31 |
+
"Area Type1": {
|
32 |
+
"Furniture1",
|
33 |
+
"Furniture2",
|
34 |
+
...
|
35 |
+
}
|
36 |
+
"Area Type2": {
|
37 |
+
"Furniture1",
|
38 |
+
"Furniture2",
|
39 |
+
...
|
40 |
+
}
|
41 |
+
}""",
|
42 |
+
):
|
43 |
+
messages = [{"role": "system", "content": system_message}]
|
44 |
+
if len(history) == 0:
|
45 |
+
pass
|
46 |
+
else:
|
47 |
+
history.pop()
|
48 |
+
|
49 |
+
for val in history:
|
50 |
+
if val[0]:
|
51 |
+
messages.append({"role": "user", "content": val[0]})
|
52 |
+
if val[1]:
|
53 |
+
messages.append({"role": "assistant", "content": val[1]})
|
54 |
+
|
55 |
+
messages.append({"role": "user", "content": message})
|
56 |
+
|
57 |
+
response = ""
|
58 |
+
|
59 |
+
for message in client.chat.completions.create(
|
60 |
+
model="meta-llama/Meta-Llama-3.1-8B-Instruct",
|
61 |
+
max_tokens=max_tokens,
|
62 |
+
stream=True,
|
63 |
+
temperature=temperature,
|
64 |
+
messages=messages,
|
65 |
+
):
|
66 |
+
token = message.choices[0].delta.content
|
67 |
+
|
68 |
+
response += token
|
69 |
+
yield response
|
70 |
+
|
71 |
+
demo = gr.ChatInterface(
|
72 |
+
respond,
|
73 |
+
additional_inputs=[
|
74 |
+
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
75 |
+
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
76 |
+
|
77 |
+
],
|
78 |
+
css=css,
|
79 |
+
theme="allenai/gradio-theme",
|
80 |
+
)
|
81 |
+
if __name__ == "__main__":
|
82 |
+
demo.launch(debug = True)
|