TestProject / app.py
andreska's picture
Make it possible to update values via API
230bfc9 verified
raw
history blame
2.15 kB
import os
import gradio as gr
from huggingface_hub import InferenceClient
api_key = os.getenv("HF_API_KEY")
client = InferenceClient(api_key=api_key)
# Create shared state for the textbox values
class State:
def __init__(self):
self.context = ""
self.question = ""
state = State()
def analyze(project_data, question):
try:
prompt = f"Analyze this project: {project_data}\n\nQuestion: {question}"
messages = [
{"role": "system", "content": f"Context: {project_data}"},
{"role": "user", "content": question}
]
response = client.chat.completions.create(
model="Qwen/Qwen2.5-72B-Instruct",
messages=messages,
max_tokens=1000,
stream=True
)
answer = ""
for chunk in response:
answer += chunk['choices'][0]['delta']['content']
yield answer
except Exception as e:
print(f"Error details: {str(e)}")
yield f"Error occurred: {str(e)}"
# Function to update textbox values
def update_values(context, question):
state.context = context
state.question = question
return state.context, state.question
with gr.Blocks() as iface:
# Create the components with the state values
project_data = gr.Textbox(label="Project Data", lines=2, value=lambda: state.context)
question = gr.Textbox(label="Question", lines=1, value=lambda: state.question)
output = gr.Textbox(label="Output")
# Create analyze button
analyze_btn = gr.Button("Analyze")
# Connect the analyze function
analyze_btn.click(
fn=analyze,
inputs=[project_data, question],
outputs=output
)
# Create an API endpoint for updating values
iface.load(fn=lambda: (state.context, state.question), outputs=[project_data, question])
gr.on(triggers=["update_values"], fn=update_values)
# Configure for external access
iface.launch(
server_name="0.0.0.0", # Allow external connections
share=True, # Create public URL
allowed_paths=["*"], # Allow CORS
)