daniloedu commited on
Commit
d9463c5
Β·
1 Parent(s): cf7efe0

Update app.py

Browse files

Updating the code to chat

Files changed (1) hide show
  1. app.py +40 -4
app.py CHANGED
@@ -1,7 +1,43 @@
 
 
 
 
1
  import gradio as gr
2
 
3
- def greet(name):
4
- return "Hello " + name + "!!"
 
5
 
6
- iface = gr.Interface(fn=greet, inputs="text", outputs="text")
7
- iface.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import requests
3
+ import random
4
+ from dotenv import load_dotenv, find_dotenv
5
  import gradio as gr
6
 
7
+ # Load environment variables
8
+ _ = load_dotenv(find_dotenv())
9
+ hf_api_key = HF_API_KEY
10
 
11
+ class Client:
12
+ def __init__(self, base_url, headers, timeout):
13
+ self.base_url = base_url
14
+ self.headers = headers
15
+ self.timeout = timeout
16
+
17
+ def generate(self, prompt, max_new_tokens):
18
+ # Your text generation code here.
19
+ return {'generated_text': 'Placeholder text'}
20
+
21
+ client = Client(HF_API_FALCOM_BASE, headers={"Authorization": f"Basic {hf_api_key}"}, timeout=120)
22
+
23
+ def generate(input, slider):
24
+ output = client.generate(input, max_new_tokens=slider).generated_text
25
+ return output
26
+
27
+ # First Gradio Interface
28
+ iface1 = gr.Interface(fn=generate, inputs=[gr.Textbox(label="Prompt"), gr.Slider(label="Max new tokens", value=20, maximum=1024, minimum=1)], outputs=[gr.Textbox(label="Completion")])
29
+
30
+ # Second Gradio Interface
31
+ def respond(message, chat_history):
32
+ bot_message = random.choice(["Tell me more about it", "Cool, but I'm not interested", "Hmmmm, ok then"])
33
+ chat_history.append((message, bot_message))
34
+ return "", chat_history
35
+
36
+ iface2 = gr.Interface(fn=respond, inputs=[gr.Textbox(label="Prompt"), gr.Chatbox(label="Chat History", height=240)], outputs=[gr.Textbox(label="Prompt"), gr.Chatbox(label="Chat History", height=240)])
37
+
38
+ # Add the other Gradio interfaces here.
39
+
40
+ if __name__ == "__main__":
41
+ iface1.launch()
42
+ iface2.launch()
43
+ # Launch the other interfaces here.