Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#import libraries here
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import random
|
| 4 |
+
from huggingface_hub import InferenceClient
|
| 5 |
+
|
| 6 |
+
#AI API being used
|
| 7 |
+
client= InferenceClient("Qwen/Qwen2.5-7B-Instruct-1M")
|
| 8 |
+
|
| 9 |
+
#defining role of AI and user
|
| 10 |
+
def respond(message,history):
|
| 11 |
+
|
| 12 |
+
messages = [{"role": "assistant", "content": "You are a friendly chatbot."}]
|
| 13 |
+
|
| 14 |
+
if history:
|
| 15 |
+
messages.extend(history) #keep adding history
|
| 16 |
+
|
| 17 |
+
messages.append({"role":"user", "content": message})
|
| 18 |
+
|
| 19 |
+
response=client.chat_completion(messages, max_tokens=100) #capping how many words the LLM is allowed to generate as a respond (100 words)
|
| 20 |
+
|
| 21 |
+
return response['choices'][0]['message']['content'].strip() #storing value of response in a readable format to display
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
#Defining chatbot giving user a UI to interact, see their conversation history, and see new messages using built in gr feature
|
| 25 |
+
#ChatInterface requires at least one parameter(a function)
|
| 26 |
+
chatbot = gr.ChatInterface(respond,type="messages", title="AI Chatbot", theme="Taithrah/Minimal")
|
| 27 |
+
|
| 28 |
+
#launching chatbot
|
| 29 |
+
chatbot.launch()
|
| 30 |
+
#You may run into errors when you're trying different models. To see the error messages, set debug to True in launch()
|