HusseinBashir commited on
Commit
a62dfe7
·
verified ·
1 Parent(s): 06e9550

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -24
app.py CHANGED
@@ -1,29 +1,23 @@
1
- import os
2
  import gradio as gr
3
- from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
4
- import torch
5
-
6
- # Read Hugging Face Token from environment
7
- hf_token = os.environ.get("HUGGINGFACE_TOKEN")
8
 
9
- # Load Somali model (private)
10
- tokenizer = AutoTokenizer.from_pretrained("cmlang/somali-flan-t5", use_auth_token=hf_token)
11
- model = AutoModelForSeq2SeqLM.from_pretrained("cmlang/somali-flan-t5", use_auth_token=hf_token)
12
 
13
- # Chatbot function
14
- def chatbot_somali(msg):
15
- inputs = tokenizer(msg, return_tensors="pt")
16
- outputs = model.generate(**inputs, max_length=150)
17
- reply = tokenizer.decode(outputs[0], skip_special_tokens=True)
18
- return reply
19
 
20
- # Gradio Interface
21
- iface = gr.Interface(
22
- fn=chatbot_somali,
23
- inputs=gr.Textbox(lines=2, placeholder="Weydii wax af Soomaali ah..."),
24
- outputs="text",
25
- title="Chatbot Af Soomaali",
26
- description="Weydii wax kasta oo af Soomaali ah — chatbot-kan wuu fahmi doonaa!"
27
- )
28
 
29
- iface.launch()
 
1
+ import openai
2
  import gradio as gr
3
+ import os
 
 
 
 
4
 
5
+ openai.api_key = os.getenv("OPENAI_API_KEY")
 
 
6
 
7
+ def chat(user_input, history=[]):
8
+ messages = [{"role": "system", "content": "Ka jawaab su’aalaha af Soomaali"}]
9
+ for q, a in history:
10
+ messages.append({"role": "user", "content": q})
11
+ messages.append({"role": "assistant", "content": a})
12
+ messages.append({"role": "user", "content": user_input})
13
 
14
+ response = openai.ChatCompletion.create(
15
+ model="gpt-4o",
16
+ messages=messages,
17
+ temperature=0.7
18
+ )
19
+ reply = response.choices[0].message.content
20
+ history.append((user_input, reply))
21
+ return history, history
22
 
23
+ gr.ChatInterface(chat, title="Chatbot Af Soomaali").launch()