Spaces:
No application file
No application file
Upload 2 files
Browse files- app (1).py +63 -0
- requirements (1).txt +3 -0
app (1).py
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
import uuid
|
| 4 |
+
|
| 5 |
+
# β
Using a public model to avoid authentication issues
|
| 6 |
+
generator = pipeline("text-generation", model="tiiuae/falcon-7b-instruct")
|
| 7 |
+
|
| 8 |
+
user_sessions = {}
|
| 9 |
+
|
| 10 |
+
def get_session(session_id):
|
| 11 |
+
if session_id not in user_sessions:
|
| 12 |
+
user_sessions[session_id] = {"chat": []}
|
| 13 |
+
return user_sessions[session_id]
|
| 14 |
+
|
| 15 |
+
def build_prompt(history):
|
| 16 |
+
prompt = "You are a helpful assistant.\n"
|
| 17 |
+
for turn in history:
|
| 18 |
+
prompt += f"User: {turn[0]}\nAssistant: {turn[1]}\n"
|
| 19 |
+
return prompt
|
| 20 |
+
|
| 21 |
+
def chat_fn(user_input, session_id):
|
| 22 |
+
session = get_session(session_id)
|
| 23 |
+
history = session["chat"]
|
| 24 |
+
prompt = build_prompt(history + [[user_input, ""]]) + "Assistant:"
|
| 25 |
+
result = generator(prompt, max_new_tokens=128, do_sample=True, temperature=0.7)[0]["generated_text"]
|
| 26 |
+
reply = result[len(prompt):].strip().split("\n")[0]
|
| 27 |
+
history.append([user_input, reply])
|
| 28 |
+
return history, session_id
|
| 29 |
+
|
| 30 |
+
def upload_handler(file):
|
| 31 |
+
try:
|
| 32 |
+
content = file.read().decode("utf-8")
|
| 33 |
+
return content
|
| 34 |
+
except Exception as e:
|
| 35 |
+
return f"Error: {str(e)}"
|
| 36 |
+
|
| 37 |
+
def code_viewer(code):
|
| 38 |
+
return "Code received. Execution is disabled on this free demo for safety."
|
| 39 |
+
|
| 40 |
+
def create_session():
|
| 41 |
+
return str(uuid.uuid4())
|
| 42 |
+
|
| 43 |
+
with gr.Blocks(title="Multi-Tool AI Chatbot") as demo:
|
| 44 |
+
session_id_state = gr.State(value=create_session())
|
| 45 |
+
|
| 46 |
+
with gr.Tab("π€ Chatbot"):
|
| 47 |
+
chatbot = gr.Chatbot()
|
| 48 |
+
msg = gr.Textbox(label="Your message")
|
| 49 |
+
send = gr.Button("Send")
|
| 50 |
+
send.click(fn=lambda msg, sid: chat_fn(msg, sid), inputs=[msg, session_id_state], outputs=[chatbot, session_id_state])
|
| 51 |
+
|
| 52 |
+
with gr.Tab("π File Reader"):
|
| 53 |
+
file_input = gr.File(label="Upload .txt file", file_types=[".txt"])
|
| 54 |
+
file_output = gr.Textbox(label="File content")
|
| 55 |
+
file_input.change(upload_handler, inputs=file_input, outputs=file_output)
|
| 56 |
+
|
| 57 |
+
with gr.Tab("π» Code Viewer"):
|
| 58 |
+
code_input = gr.Code(language="python", label="Enter Python code")
|
| 59 |
+
code_output = gr.Textbox(label="Output")
|
| 60 |
+
run_btn = gr.Button("View Output")
|
| 61 |
+
run_btn.click(fn=code_viewer, inputs=code_input, outputs=code_output)
|
| 62 |
+
|
| 63 |
+
demo.launch()
|
requirements (1).txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
transformers
|
| 2 |
+
torch
|
| 3 |
+
gradio
|