Spaces:
Runtime error
Runtime error
Commit
·
d6df619
1
Parent(s):
f452408
Initial commit
Browse files- Dockerfile +12 -0
- app.py +51 -0
- requirements.txt +4 -0
Dockerfile
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
FROM python:3.9
|
| 3 |
+
|
| 4 |
+
WORKDIR /code
|
| 5 |
+
|
| 6 |
+
COPY ./requirements.txt /code/requirements.txt
|
| 7 |
+
|
| 8 |
+
RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
|
| 9 |
+
|
| 10 |
+
COPY . .
|
| 11 |
+
|
| 12 |
+
CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "7860"]
|
app.py
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import requests
|
| 3 |
+
from dotenv import load_dotenv
|
| 4 |
+
import gradio as gr
|
| 5 |
+
import random
|
| 6 |
+
from text_generation import Client # Assumed custom package
|
| 7 |
+
|
| 8 |
+
# Load environment variables
|
| 9 |
+
load_dotenv()
|
| 10 |
+
hf_api_key = os.environ['HF_API_KEY']
|
| 11 |
+
|
| 12 |
+
# Initialize the client
|
| 13 |
+
client = Client(os.environ['HF_API_FALCOM_BASE'], headers={"Authorization": f"Basic {hf_api_key}"}, timeout=120)
|
| 14 |
+
|
| 15 |
+
# Text generation function
|
| 16 |
+
def generate(input_text, max_tokens):
|
| 17 |
+
return client.generate(input_text, max_new_tokens=max_tokens).generated_text
|
| 18 |
+
|
| 19 |
+
# Gradio interface for text generation
|
| 20 |
+
demo_text_gen = gr.Interface(fn=generate, inputs=[gr.Textbox(label="Prompt"), gr.Slider(label="Max new tokens", value=20, maximum=1024, minimum=1)], outputs=gr.Textbox(label="Generated Text"))
|
| 21 |
+
|
| 22 |
+
# Chat history management
|
| 23 |
+
def format_chat_prompt(message, chat_history):
|
| 24 |
+
prompt = ""
|
| 25 |
+
for user_msg, bot_msg in chat_history:
|
| 26 |
+
prompt += f"\nUser: {user_msg}\nAssistant: {bot_msg}"
|
| 27 |
+
return f"{prompt}\nUser: {message}\nAssistant:"
|
| 28 |
+
|
| 29 |
+
# Chatbot response generation
|
| 30 |
+
def respond(message, chat_history, instruction, temperature=0.7):
|
| 31 |
+
prompt = format_chat_prompt(message, chat_history, instruction)
|
| 32 |
+
response = client.generate(prompt, max_new_tokens=1024, stop_sequences=["\nUser:", ""], temperature=temperature)
|
| 33 |
+
chat_history.append((message, response.generated_text))
|
| 34 |
+
return response.generated_text, chat_history
|
| 35 |
+
|
| 36 |
+
# Gradio interface for chatbot
|
| 37 |
+
with gr.Blocks() as demo_chatbot:
|
| 38 |
+
chatbot = gr.Chatbot()
|
| 39 |
+
msg = gr.Textbox(label="Your Message")
|
| 40 |
+
system_msg = gr.Textbox(label="System Instruction", value="A conversation with an AI.")
|
| 41 |
+
temperature_slider = gr.Slider(label="Temperature", minimum=0.1, maximum=1, value=0.7)
|
| 42 |
+
submit_btn = gr.Button("Send")
|
| 43 |
+
chat_history = []
|
| 44 |
+
submit_btn.click(respond, inputs=[msg, chat_history, system_msg, temperature_slider], outputs=[chatbot])
|
| 45 |
+
msg.submit(respond, inputs=[msg, chat_history, system_msg, temperature_slider], outputs=[chatbot])
|
| 46 |
+
|
| 47 |
+
# Launch Gradio apps
|
| 48 |
+
if __name__ == "__main__":
|
| 49 |
+
gr.close_all()
|
| 50 |
+
demo_text_gen.launch(server_port=int(os.environ.get('PORT1', 7860)))
|
| 51 |
+
demo_chatbot.launch(server_port=int(os.environ.get('PORT2', 7861)))
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
requests==2.28.1
|
| 2 |
+
python-dotenv==0.20.0
|
| 3 |
+
gradio==3.1.4
|
| 4 |
+
Pillow==9.1.1
|