Spaces:
Running
Running
Upload app.py with huggingface_hub
Browse files
app.py
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from openai import OpenAI
|
| 3 |
+
import gradio as gr
|
| 4 |
+
|
| 5 |
+
client = OpenAI(
|
| 6 |
+
api_key=os.environ["GROQ_API_KEY"],
|
| 7 |
+
base_url="https://api.groq.com/openai/v1",
|
| 8 |
+
)
|
| 9 |
+
|
| 10 |
+
class GroqLLM:
|
| 11 |
+
def __init__(self, client, model_name):
|
| 12 |
+
self.client = client
|
| 13 |
+
self.model_name = model_name
|
| 14 |
+
self.memory_history = []
|
| 15 |
+
|
| 16 |
+
def predict(self, user_message):
|
| 17 |
+
prompt = "You are a helpful assistant.\n"
|
| 18 |
+
for msg in self.memory_history:
|
| 19 |
+
prompt += f"{msg}\n"
|
| 20 |
+
prompt += f"User: {user_message}\nAssistant:"
|
| 21 |
+
|
| 22 |
+
response = self.client.responses.create(
|
| 23 |
+
model=self.model_name,
|
| 24 |
+
input=prompt,
|
| 25 |
+
max_output_tokens=300
|
| 26 |
+
)
|
| 27 |
+
|
| 28 |
+
try:
|
| 29 |
+
answer = response.output_text
|
| 30 |
+
except:
|
| 31 |
+
answer = str(response)
|
| 32 |
+
|
| 33 |
+
self.memory_history.append(f"User: {user_message}")
|
| 34 |
+
self.memory_history.append(f"Assistant: {answer}")
|
| 35 |
+
if len(self.memory_history) > 20:
|
| 36 |
+
self.memory_history = self.memory_history[-20:]
|
| 37 |
+
return answer
|
| 38 |
+
|
| 39 |
+
llm = GroqLLM(client, "openai/gpt-oss-20b")
|
| 40 |
+
|
| 41 |
+
def chat_func(msg, history):
|
| 42 |
+
return llm.predict(msg)
|
| 43 |
+
|
| 44 |
+
demo = gr.ChatInterface(chat_func)
|
| 45 |
+
|
| 46 |
+
demo.launch()
|