Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -0,0 +1,80 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import pipeline, AutoTokenizer, AutoModelForSequenceClassification
|
| 3 |
+
from groq import Groq
|
| 4 |
+
import os
|
| 5 |
+
|
| 6 |
+
# Set Groq API key
|
| 7 |
+
os.environ["GROQ_API_KEY"] = "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"
|
| 8 |
+
|
| 9 |
+
# Text Classification Setup
|
| 10 |
+
classifier = pipeline("zero-shot-classification",
|
| 11 |
+
model="facebook/bart-large-mnli")
|
| 12 |
+
|
| 13 |
+
# Chatbot Setup
|
| 14 |
+
client = Groq()
|
| 15 |
+
system_prompt = """You are an advanced AI assistant with deep contextual understanding.
|
| 16 |
+
Maintain natural conversation while demonstrating:
|
| 17 |
+
1. Complex sentence comprehension
|
| 18 |
+
2. Contextual awareness across multiple turns
|
| 19 |
+
3. Emotional intelligence
|
| 20 |
+
4. Domain-specific knowledge adaptation"""
|
| 21 |
+
|
| 22 |
+
def classify_text(text, labels):
|
| 23 |
+
labels = [label.strip() for label in labels.split(",")]
|
| 24 |
+
results = classifier(text, labels, multi_label=False)
|
| 25 |
+
return {label: score for label, score in zip(results["labels"], results["scores"])}
|
| 26 |
+
|
| 27 |
+
def groq_chat(user_input, history):
|
| 28 |
+
conversation = [{"role": "system", "content": system_prompt}]
|
| 29 |
+
|
| 30 |
+
for user, assistant in history:
|
| 31 |
+
conversation.extend([
|
| 32 |
+
{"role": "user", "content": user},
|
| 33 |
+
{"role": "assistant", "content": assistant}
|
| 34 |
+
])
|
| 35 |
+
|
| 36 |
+
conversation.append({"role": "user", "content": user_input})
|
| 37 |
+
|
| 38 |
+
response = client.chat.completions.create(
|
| 39 |
+
model="llama3-70b-8192",
|
| 40 |
+
messages=conversation,
|
| 41 |
+
temperature=0.7,
|
| 42 |
+
max_tokens=512,
|
| 43 |
+
top_p=1
|
| 44 |
+
)
|
| 45 |
+
|
| 46 |
+
return response.choices[0].message.content
|
| 47 |
+
|
| 48 |
+
# Gradio Interface
|
| 49 |
+
with gr.Blocks() as app:
|
| 50 |
+
gr.Markdown("# Advanced LLM Application")
|
| 51 |
+
|
| 52 |
+
with gr.Tab("Text Classification"):
|
| 53 |
+
with gr.Row():
|
| 54 |
+
with gr.Column():
|
| 55 |
+
text_input = gr.Textbox(label="Input Text")
|
| 56 |
+
labels_input = gr.Textbox(label="Categories (comma-separated)",
|
| 57 |
+
value="positive, negative, neutral")
|
| 58 |
+
classify_btn = gr.Button("Classify")
|
| 59 |
+
results_output = gr.Label(label="Classification Results")
|
| 60 |
+
|
| 61 |
+
classify_btn.click(
|
| 62 |
+
fn=classify_text,
|
| 63 |
+
inputs=[text_input, labels_input],
|
| 64 |
+
outputs=results_output
|
| 65 |
+
)
|
| 66 |
+
|
| 67 |
+
with gr.Tab("Chatbot"):
|
| 68 |
+
chatbot = gr.Chatbot(height=400)
|
| 69 |
+
msg = gr.Textbox(label="Your Message")
|
| 70 |
+
clear = gr.Button("Clear")
|
| 71 |
+
|
| 72 |
+
def respond(message, chat_history):
|
| 73 |
+
bot_message = groq_chat(message, chat_history)
|
| 74 |
+
chat_history.append((message, bot_message))
|
| 75 |
+
return "", chat_history
|
| 76 |
+
|
| 77 |
+
msg.submit(respond, [msg, chatbot], [msg, chatbot])
|
| 78 |
+
clear.click(lambda: None, None, chatbot, queue=False)
|
| 79 |
+
|
| 80 |
+
app.launch()
|