Spaces:
Runtime error
Runtime error
Update app.py
Browse files16 unmodified lines171819202122232425262728293081 unmodified lines11211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618717 unmodified lines20520620720820921021116 unmodified lines# Environment variables and configurationshuggingface_token = os.environ.get("HUGGINGFACE_TOKEN")ACCOUNT_ID = os.environ.get("CLOUDFARE_ACCOUNT_ID")API_TOKEN = os.environ.get("CLOUDFLARE_AUTH_TOKEN")API_BASE_URL = "https://api.cloudflare.com/client/v4/accounts/a17f03e0f049ccae0c15cdcf3b9737ce/ai/run/"MODELS = ["mistralai/Mistral-7B-Instruct-v0.3","mistralai/Mixtral-8x7B-Instruct-v0.1","@cf/meta/llama-3.1-8b-instruct","mistralai/Mistral-Nemo-Instruct-2407","meta-llama/Meta-Llama-3.1-8B-Instruct","meta-llama/Meta-Llama-3.1-70B-Instruct"81 unmodified linesWrite a detailed and complete research document that fulfills the following user request: '{query}'After writing the document, please provide a list of sources used in your response."""if model == "@cf/meta/llama-3.1-8b-instruct":# Use Cloudflare APIfor response in get_response_from_cloudflare(prompt="", context=context, query=query, num_calls=num_calls, temperature=temperature):yield response, "" # Yield streaming response without sourceselse:# Use Hugging Face APIclient = InferenceClient(model, token=huggingface_token)main_content = ""for i in range(num_calls):for message in client.chat_completion(messages=[{"role": "user", "content": prompt}],max_tokens=10000,temperature=temperature,stream=True,):if message.choices and message.choices[0].delta and message.choices[0].delta.content:chunk = message.choices[0].delta.contentmain_content += chunkyield main_content, "" # Yield partial main content without sourcesdef get_response_from_cloudflare(prompt, context, query, num_calls=3, temperature=0.2):headers = {"Authorization": f"Bearer {API_TOKEN}","Content-Type": "application/json"}model = "@cf/meta/llama-3.1-8b-instruct"instruction = f"""Using the following context:{context}Write a detailed and complete research document that fulfills the following user request: '{query}'After writing the document, please provide a list of sources used in your response."""inputs = [{"role": "system", "content": instruction},{"role": "user", "content": query}]payload = {"messages": inputs,"stream": True,"temperature": temperature,"max_tokens": 32000}full_response = ""for i in range(num_calls):try:with requests.post(f"{API_BASE_URL}{model}", headers=headers, json=payload, stream=True) as response:if response.status_code == 200:for line in response.iter_lines():if line:try:json_response = json.loads(line.decode('utf-8').split('data: ')[1])if 'response' in json_response:chunk = json_response['response']full_response += chunkyield full_responseexcept (json.JSONDecodeError, IndexError) as e:logging.error(f"Error parsing streaming response: {str(e)}")continueelse:logging.error(f"HTTP Error: {response.status_code}, Response: {response.text}")yield f"I apologize, but I encountered an HTTP error: {response.status_code}. Please try again later."except Exception as e:logging.error(f"Error in generating response from Cloudflare: {str(e)}")yield f"I apologize, but an error occurred: {str(e)}. Please try again later."if not full_response:yield "I apologize, but I couldn't generate a response at this time. Please try again later."def vote(data: gr.LikeData):if data.liked:17 unmodified linesdemo = gr.ChatInterface(respond,additional_inputs=[gr.Dropdown(choices=MODELS, label="Select Model", value=MODELS[3]),gr.Slider(minimum=0.1, maximum=1.0, value=0.2, step=0.1, label="Temperature"),gr.Slider(minimum=1, maximum=5, value=1, step=1, label="Number of API Calls"),],16 unmodified lines1718192021222324252681 unmodified lines10810911011111211311411511611711811912012112212312412512612712817 unmodified lines14614714814915015115216 unmodified lines# Environment variables and configurationshuggingface_token = os.environ.get("HUGGINGFACE_TOKEN")MODELS = ["mistralai/Mistral-7B-Instruct-v0.3","mistralai/Mixtral-8x7B-Instruct-v0.1","mistralai/Mistral-Nemo-Instruct-2407","meta-llama/Meta-Llama-3.1-8B-Instruct","meta-llama/Meta-Llama-3.1-70B-Instruct"81 unmodified linesWrite a detailed and complete research document that fulfills the following user request: '{query}'After writing the document, please provide a list of sources used in your response."""# Use Hugging Face APIclient = InferenceClient(model, token=huggingface_token)main_content = ""for i in range(num_calls):for message in client.chat_completion(messages=[{"role": "user", "content": prompt}],max_tokens=10000,temperature=temperature,stream=True,):if message.choices and message.choices[0].delta and message.choices[0].delta.content:chunk = message.choices[0].delta.contentmain_content += chunkyield main_content, "" # Yield partial main content without sourcesdef vote(data: gr.LikeData):if data.liked:17 unmodified linesdemo = gr.ChatInterface(respond,additional_inputs=[gr.Dropdown(choices=MODELS, label="Select Model", value=MODELS[2]),gr.Slider(minimum=0.1, maximum=1.0, value=0.2, step=0.1, label="Temperature"),gr.Slider(minimum=1, maximum=5, value=1, step=1, label="Number of API Calls"),],
app.py+15-74