Spaces:
Runtime error
Runtime error
avinash commited on
Commit ·
e489749
1
Parent(s): f739f6b
firststeps
Browse files- .gitignore +1 -0
- app.py +57 -0
- requirements.txt +4 -0
.gitignore
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
.env
|
app.py
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from dotenv import load_dotenv
|
| 3 |
+
from openai import OpenAI
|
| 4 |
+
import os
|
| 5 |
+
|
| 6 |
+
load_dotenv()
|
| 7 |
+
|
| 8 |
+
API_KEY = os.getenv("OPENAI_API_KEY")
|
| 9 |
+
MODEL_NAME = os.getenv("MODEL_NAME")
|
| 10 |
+
BASE_URL = os.getenv("BASE_URL")
|
| 11 |
+
|
| 12 |
+
client = OpenAI(
|
| 13 |
+
api_key=API_KEY,
|
| 14 |
+
base_url=BASE_URL,
|
| 15 |
+
)
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
def chat_with_gemma(history, message):
|
| 19 |
+
messages = [{"role": "system", "content": "you are an helpful assistant who helps users to find products on the internet"}]
|
| 20 |
+
for user_msg, bot_msg in history:
|
| 21 |
+
messages.append({"role": "user", "content": user_msg})
|
| 22 |
+
messages.append({"role": "assistant", "content": bot_msg})
|
| 23 |
+
messages.append({"role": "user", "content": message})
|
| 24 |
+
|
| 25 |
+
try:
|
| 26 |
+
response = client.chat.completions.create(
|
| 27 |
+
model=MODEL_NAME,
|
| 28 |
+
messages=messages,
|
| 29 |
+
temperature=0.7,
|
| 30 |
+
max_tokens=512,
|
| 31 |
+
)
|
| 32 |
+
reply = response.choices[0].message.content.strip()
|
| 33 |
+
except Exception as e:
|
| 34 |
+
reply = f"⚠️ Error: {str(e)}"
|
| 35 |
+
|
| 36 |
+
# Return updated history
|
| 37 |
+
history.append((message, reply))
|
| 38 |
+
return history, ""
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
with gr.Blocks(title="Gemma Chat - MCP Tool") as demo:
|
| 42 |
+
gr.Markdown("## 🧠 Gemma LLM Chatbot via Modal (MCP Tool)")
|
| 43 |
+
with gr.Row():
|
| 44 |
+
chatbot = gr.Chatbot()
|
| 45 |
+
with gr.Row():
|
| 46 |
+
msg = gr.Textbox(label="Your message here...", placeholder="Ask something...")
|
| 47 |
+
with gr.Row():
|
| 48 |
+
clear = gr.Button("🔄 Clear Chat")
|
| 49 |
+
|
| 50 |
+
state = gr.State([])
|
| 51 |
+
|
| 52 |
+
msg.submit(chat_with_gemma, [state, msg], [chatbot, msg])
|
| 53 |
+
clear.click(lambda: ([], ""), outputs=[chatbot, msg])
|
| 54 |
+
|
| 55 |
+
# ✅ Launch the app
|
| 56 |
+
if __name__ == "__main__":
|
| 57 |
+
demo.launch(debug=True)
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio[mcp]
|
| 2 |
+
modal
|
| 3 |
+
openai
|
| 4 |
+
python-dotenv
|