Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -46,50 +46,38 @@ import gradio as gr
|
|
| 46 |
from huggingface_hub import InferenceClient
|
| 47 |
import os
|
| 48 |
|
|
|
|
| 49 |
hf_token = os.getenv("HF_TOKEN")
|
| 50 |
client = InferenceClient(api_key=hf_token)
|
| 51 |
|
| 52 |
-
|
| 53 |
-
|
| 54 |
messages = [{"role": "user", "content": input_text}]
|
|
|
|
|
|
|
|
|
|
| 55 |
|
| 56 |
-
# Add conversation history (if exists)
|
| 57 |
-
if history:
|
| 58 |
-
for user_input, bot_response in history:
|
| 59 |
-
messages.append({"role": "user", "content": user_input})
|
| 60 |
-
messages.append({"role": "assistant", "content": bot_response})
|
| 61 |
-
|
| 62 |
-
# Generate model response
|
| 63 |
stream = client.chat.completions.create(
|
| 64 |
model="google/gemma-2-2b-it",
|
| 65 |
messages=messages,
|
| 66 |
-
temperature=0.5,
|
| 67 |
-
max_tokens=2048,
|
| 68 |
-
top_p=0.7,
|
| 69 |
stream=True
|
| 70 |
)
|
| 71 |
|
| 72 |
-
#
|
| 73 |
-
bot_response = ""
|
| 74 |
-
for chunk in stream:
|
| 75 |
-
bot_response += chunk.choices[0].delta.content
|
| 76 |
-
|
| 77 |
-
# Update the conversation history
|
| 78 |
history.append((input_text, bot_response))
|
| 79 |
return bot_response, history
|
| 80 |
|
| 81 |
-
#
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
bot_response, state = chatbot(user_input, state)
|
| 89 |
-
chatbot_ui.append((user_input, bot_response))
|
| 90 |
-
return chatbot_ui, state
|
| 91 |
-
|
| 92 |
-
text_input.submit(user_input_handler, [text_input, chatbot_ui, state], [chatbot_ui, state])
|
| 93 |
|
| 94 |
-
#
|
| 95 |
demo.launch()
|
|
|
|
| 46 |
from huggingface_hub import InferenceClient
|
| 47 |
import os
|
| 48 |
|
| 49 |
+
# Initialize Hugging Face Inference Client
|
| 50 |
hf_token = os.getenv("HF_TOKEN")
|
| 51 |
client = InferenceClient(api_key=hf_token)
|
| 52 |
|
| 53 |
+
# Function to handle user inputs and fetch model responses
|
| 54 |
+
def chatbot(input_text, history=[]):
|
| 55 |
messages = [{"role": "user", "content": input_text}]
|
| 56 |
+
for user_input, bot_response in history:
|
| 57 |
+
messages.append({"role": "user", "content": user_input})
|
| 58 |
+
messages.append({"role": "assistant", "content": bot_response})
|
| 59 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
stream = client.chat.completions.create(
|
| 61 |
model="google/gemma-2-2b-it",
|
| 62 |
messages=messages,
|
| 63 |
+
#temperature=0.5,
|
| 64 |
+
#max_tokens=2048,
|
| 65 |
+
#top_p=0.7,
|
| 66 |
stream=True
|
| 67 |
)
|
| 68 |
|
| 69 |
+
# Concatenate streamed response
|
| 70 |
+
bot_response = "".join(chunk.choices[0].delta.content for chunk in stream)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
history.append((input_text, bot_response))
|
| 72 |
return bot_response, history
|
| 73 |
|
| 74 |
+
# Gradio Interface
|
| 75 |
+
demo = gr.Interface(
|
| 76 |
+
fn=chatbot,
|
| 77 |
+
inputs=["text", "state"],
|
| 78 |
+
outputs=["text", "state"],
|
| 79 |
+
title="Gemma Chatbot"
|
| 80 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
|
| 82 |
+
# Launch Gradio App
|
| 83 |
demo.launch()
|