Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,97 +1,32 @@
|
|
| 1 |
-
|
| 2 |
import gradio as gr
|
| 3 |
-
from
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
""
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
for message in client.chat_completion(
|
| 35 |
-
messages,
|
| 36 |
-
max_tokens=max_tokens,
|
| 37 |
-
stream=True,
|
| 38 |
-
temperature=temperature,
|
| 39 |
-
top_p=top_p,
|
| 40 |
-
):
|
| 41 |
-
# Ensure the message has a valid structure
|
| 42 |
-
if not message or not isinstance(message, dict):
|
| 43 |
-
continue
|
| 44 |
-
|
| 45 |
-
try:
|
| 46 |
-
# Extract content and finish reason
|
| 47 |
-
content = message.choices[0].delta.content
|
| 48 |
-
finish_reason = message.choices[0].finish_reason
|
| 49 |
-
|
| 50 |
-
# Check if the content is empty
|
| 51 |
-
if content.strip() == "":
|
| 52 |
-
# If the finish reason is 'stop', it's expected and we can break the loop
|
| 53 |
-
if finish_reason == "stop":
|
| 54 |
-
print("Stream ended normally.")
|
| 55 |
-
break
|
| 56 |
-
else:
|
| 57 |
-
print("Received unexpected empty content, skipping...")
|
| 58 |
-
continue
|
| 59 |
-
|
| 60 |
-
response += content
|
| 61 |
-
yield response
|
| 62 |
-
|
| 63 |
-
except (AttributeError, IndexError, KeyError) as e:
|
| 64 |
-
print(f"Error processing message: {e}")
|
| 65 |
-
continue
|
| 66 |
-
|
| 67 |
-
except Exception as e:
|
| 68 |
-
print(f"Unexpected error: {e}")
|
| 69 |
-
yield "An error occurred while generating the response."
|
| 70 |
-
|
| 71 |
-
# Final check if the response is empty
|
| 72 |
-
if response.strip() == "":
|
| 73 |
-
yield "No response generated. Please try again or adjust the settings."
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
"""
|
| 77 |
-
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
| 78 |
-
"""
|
| 79 |
-
demo = gr.ChatInterface(
|
| 80 |
-
respond,
|
| 81 |
-
additional_inputs=[
|
| 82 |
-
gr.Textbox(value="You are a friendly Chatbot. Your name is Juninho.", label="System message"),
|
| 83 |
-
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 84 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 85 |
-
gr.Slider(
|
| 86 |
-
minimum=0.1,
|
| 87 |
-
maximum=1.0,
|
| 88 |
-
value=0.95,
|
| 89 |
-
step=0.05,
|
| 90 |
-
label="Top-p (nucleus sampling)",
|
| 91 |
-
),
|
| 92 |
-
],
|
| 93 |
-
)
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
if __name__ == "__main__":
|
| 97 |
-
demo.launch()
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from transformers import LlamaForCausalLM, LlamaTokenizer
|
| 3 |
+
import torch
|
| 4 |
+
|
| 5 |
+
class Chatbot:
|
| 6 |
+
def __init__(self, model_name="meta-llama/Llama-3.1-8B"):
|
| 7 |
+
self.tokenizer = LlamaTokenizer.from_pretrained(model_name)
|
| 8 |
+
self.model = LlamaForCausalLM.from_pretrained(model_name)
|
| 9 |
+
|
| 10 |
+
def respond(self, prompt):
|
| 11 |
+
inputs = self.tokenizer.encode(prompt + self.tokenizer.eos_token, return_tensors="pt")
|
| 12 |
+
outputs = self.model.generate(
|
| 13 |
+
inputs,
|
| 14 |
+
max_length=150,
|
| 15 |
+
num_return_sequences=1,
|
| 16 |
+
no_repeat_ngram_size=2,
|
| 17 |
+
top_k=50,
|
| 18 |
+
top_p=0.95,
|
| 19 |
+
temperature=0.7,
|
| 20 |
+
pad_token_id=self.tokenizer.eos_token_id
|
| 21 |
+
)
|
| 22 |
+
response = self.tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 23 |
+
return response
|
| 24 |
+
|
| 25 |
+
chatbot = Chatbot()
|
| 26 |
+
|
| 27 |
+
def chatbot_interface(user_input):
|
| 28 |
+
return chatbot.respond(user_input)
|
| 29 |
+
|
| 30 |
+
interface = gr.Interface(fn=chatbot_interface, inputs="text", outputs="text", title="Chatbot LLaMA", description="Converse com seu chatbot!")
|
| 31 |
+
|
| 32 |
+
interface.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|